﻿using System.Windows;
using System.Windows.Controls;

using Game.Surface;
using Game.Level;
using Game.Controller;

namespace SL
{
    public partial class GamePage 
    {
        AppSurfaceControl _SurfaceControl;

        public GamePage()
        {
            InitializeComponent();
            Init();

            slider1.Value = 50;
            infoControl1.GameMap = _SurfaceControl.GameMapView;
            infoControl1.GameController = _SurfaceControl.GameController;

            Loaded += delegate 
            {
                _SurfaceControl.GameController.Start();
            };
        }

        public AppSurfaceControl SurfaceControl
        {
            get { return _SurfaceControl; }
        }
        
        void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (_SurfaceControl != null)
            {
                _SurfaceControl.GameScale = (e.NewValue / 100d);
            }
        }
        
        void Init()
        {
            GameLevels levels = Game.Toolkit.StandardGame.Level1;
            Init(levels);
        }

        private void Init(GameLevels levels)
        {
            infoControl1.GameLevels = levels;
            {
                GameLevel level = levels[0];
                var controller = new GameController(level.GameMap, levels);
                _SurfaceControl = new AppSurfaceControl(levels, level, controller);

                infoControl1.GameMap = _SurfaceControl.GameMapView;
                infoControl1.GameController = _SurfaceControl.GameController;
                //bottomPanelControl1.GameController = _SurfaceControl.GameController;
                levels.Events.FireLevelActive(level);

                Canvas.SetLeft(_SurfaceControl, 10);
                Canvas.SetTop(_SurfaceControl, 10);
                _SurfaceControl.GameScale = (slider1.Value / 100d);

                canvas2.Children.Add(_SurfaceControl);
            }

            _SurfaceControl.NextLevel += delegate
            {
                canvas2.Children.Remove(_SurfaceControl);

                GameLevel level = levels[1];
                var c = new GameController(level.GameMap, levels);
                _SurfaceControl = new AppSurfaceControl(levels, level, c);

                Canvas.SetLeft(_SurfaceControl, 10);
                Canvas.SetTop(_SurfaceControl, 10);
                _SurfaceControl.GameScale = (slider1.Value / 100d);

                canvas2.Children.Add(_SurfaceControl);
                infoControl1.GameController = _SurfaceControl.GameController;
                infoControl1.GameMap = _SurfaceControl.GameMapView;
                //bottomPanelControl1.GameController = _SurfaceControl.GameController;

                levels.Events.FireLevelActive(level);

                _SurfaceControl.GameController.Start();
            };

            levels.Events.GameEnded += delegate
            {
                canvas2.Children.Remove(_SurfaceControl);
                infoControl1.GameEnded();
            };

            levels.Events.GameRestart += delegate
            {
                GameLevels levels2 = Game.Toolkit.StandardGame.Level1;
                Init(levels2);
                _SurfaceControl.GameController.Start();
            };
        }

        private void UserControl_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if ("" + e.Key == "C")
            {
                var w = new CommandDlg(_SurfaceControl);
                w.Show();
            }
        }

    }
}
